/**
 * StudentAnnotation.java
 *
 * This program retrieves the class level annotations declared with Student class.
 *
 * Copyright 2007 Aptech Software Limited. All rights reserved.
 */

package customstudentannotation;

import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
 * RetentionPolicy.RUNTIME allows specifying that the annotation
 * should be available for retention at runtime.
 */
@Retention(RetentionPolicy.RUNTIME)

/**
 * Declare custom annotation StringValue with parameters.
 */
@interface StringValue {
    
    /**
     * String parameter to store the name.
     */
    String str();
    
    /**
     * Integer parameter to store the value.
     */
    int val();
}

/**
 * RetentionPolicy.RUNTIME allows specifying that the annotation
 * should be available for retention at runtime.
 */
@Retention(RetentionPolicy.RUNTIME)

/**
 * Declare custom annotation description with a parameter.
 */
@interface Description {
    
    /**
     * String parameter to store the description.
     */
    String description();
}

/**
 * StudentAnnotation class retrieves the class level annotations declared with
 * Student class.
 */
public class StudentAnnotation {
    
    /**
     * This is the entry point for the CustomStudentAnnotation application.
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        createObject();
    }
    
    /**
     * Declare method level annotation.
     */
    @Description(description = "An annotation test method")
    @StringValue(str = "Testing", val = 100)
    
    /**
     * This method is invoked to create Student instance, and then display all
     * annotations of Student class.
     */
    public static void createObject() {
        
        Student std = new Student("Clara", "254-B, Hill Street, New York", 20);
        
        /**
         * Retrieve class level annotations by first retrieving the Student
         * class, and then retrieving class level annotations in Annotation
         * array instances.
         */
        Annotation annos[] = std.getClass().getAnnotations();
        
        System.out.println("\nAll annotations for Student: ");
        
        // Iterate the loop through Annotation array instances to display
        // all class level annotations for the Student class
        for(Annotation a : annos)
            System.out.println(a);
    }
    
}
